package com.sifeng.test;

import com.sifeng.pojo.WorkloadRatioDTO;
import com.sifeng.utils.MyUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang_gsh on 2018/10/30.
 * 查询需求功能点数及比例
 */
public class QueryRequirementFunctionPoint {
	public static List<WorkloadRatioDTO> execute(String path) throws IOException, InvalidFormatException, ParseException {
		List<String> fileList = MyUtils.searchFile(path, "REQ-", "原子功能点评估表.xlsx");
		List<WorkloadRatioDTO> workloadRatioDTOList = new ArrayList<WorkloadRatioDTO>();
		for (String s : fileList) {
			WorkloadRatioDTO workloadRatioDTO = getPoint(path + s);
			int workloadRatioCountPoint = workloadRatioDTO.getCountPoint();
			String workloadRatioPointRatio = workloadRatioDTO.getPointRatio();
			String[] pointRatioSplit = workloadRatioPointRatio.split("；");
			for (String s1 : pointRatioSplit) {
				if(s1.contains("营销服务研发部")) {
					String[] s1Split = s1.split("：");
					NumberFormat nf = NumberFormat.getPercentInstance();
					Number m = nf.parse(s1Split[1]);
					double dd = m.doubleValue();
					workloadRatioDTO.setWorkloadOfOurDepartment(workloadRatioCountPoint * dd);
					workloadRatioDTOList.add(workloadRatioDTO);
					break;
				}
			}
		}

		return workloadRatioDTOList;
	}
	public static WorkloadRatioDTO getPoint(String excelPath) throws IOException, InvalidFormatException {
		InputStream iStream=new FileInputStream(excelPath);
		XSSFWorkbook wb = new XSSFWorkbook(iStream);
		XSSFSheet sheet = wb.getSheetAt(0);
		if (sheet == null) {
			return null;
		}

		XSSFRow row = sheet.getRow(6);
		XSSFCell cell = row.getCell(10);
		cell.setCellType(Cell.CELL_TYPE_STRING);
		String countPointStr = cell.getStringCellValue();
		int countPoint = Integer.parseInt(countPointStr.trim());

		XSSFRow row1 = sheet.getRow(3);
		XSSFCell cell1 = row1.getCell(1);
		cell1.setCellType(Cell.CELL_TYPE_STRING);
		String pointRatio = cell1.getStringCellValue();

		XSSFRow row2 = sheet.getRow(1);
		XSSFCell cell2 = row2.getCell(3);
		cell2.setCellType(Cell.CELL_TYPE_STRING);
		String requirementNumber = cell2.getStringCellValue();

		XSSFRow row3 = sheet.getRow(1);
		XSSFCell cell3 = row3.getCell(1);
		cell3.setCellType(Cell.CELL_TYPE_STRING);
		String requirementName = cell3.getStringCellValue();

		WorkloadRatioDTO workloadRatioDTO = new WorkloadRatioDTO();
		workloadRatioDTO.setCountPoint(countPoint);
		workloadRatioDTO.setPointRatio(pointRatio);
		workloadRatioDTO.setRequirementNumber(requirementNumber);
		workloadRatioDTO.setRequirementName(requirementName);

		return workloadRatioDTO;
	}

	public static void main(String[] args) throws IOException, InvalidFormatException, ParseException {
		String path = "E:\\hisun\\开发三部\\功能点评估表\\201902\\";
		List<WorkloadRatioDTO> workloadRatioDTOList = QueryRequirementFunctionPoint.execute(path);
		DecimalFormat df = new DecimalFormat("0");
		for (WorkloadRatioDTO workloadRatioDTO : workloadRatioDTOList) {
			System.out.println("需求编号:" + workloadRatioDTO.getRequirementNumber() + ",需求名称:" + workloadRatioDTO.getRequirementName() + ",总工作量:" +
					workloadRatioDTO.getCountPoint() + ",本部门工作量:" + df.format(workloadRatioDTO.getWorkloadOfOurDepartment()) +
					",比例:" + workloadRatioDTO.getPointRatio());
		}
	}
}
